//
//  SEPrescriptionPickDrugsViewController.swift
//  SmartEyeForDoctor
//
//  Created by QDSG on 2021/5/11.
//

import UIKit

class SEPrescriptionPickDrugsViewController: SEBaseViewController {
    
    private var selectedHandler: ((SEPrescriptionDrugPickViewModel) -> Void)?
    
    private var viewModel = SEPrescriptionDrugPickViewModel()
    
    convenience init(selectedHandler: ((SEPrescriptionDrugPickViewModel) -> Void)?) {
        self.init()
        self.selectedHandler = selectedHandler
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        drugsListView.viewModel = viewModel
        
        drugsListView.selectedHandler = { [weak self] in
            guard let self = self else { return }
            self.drugsDetailView.viewModel = self.viewModel
            self.transitionAnimation(.fromRight)
        }
        
        drugsListView.dismissHandler = { [weak self] in
            self?.dismissVc()
        }
        
        drugsDetailView.backHandler = { [weak self] in
            self?.transitionAnimation(.fromLeft)
        }
        
        drugsDetailView.confirmHandler = { [weak self] in
            self?.drugsDetailView.endEditing(true)
            guard let self = self else { return }
            if self.viewModel.confirmEnabled == true {
                self.selectedHandler?(self.viewModel)
                self.dismissVc()
            } else {
                SEHUD.show(message: "请填写药品完整信息")
            }
        }
    }
    
    override func setupUI() {
        view.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        
        view.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        view.addSubview(containerView)
        containerView.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(540)
            make.centerY.equalToSuperview()
        }
        
        containerView.insertSubview(drugsListView, at: 1)
        drugsListView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        containerView.insertSubview(drugsDetailView, at: 0)
        drugsDetailView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        drugsDetailView.selectedHandler = { [weak self] cellType in
            self?.drugsDetailView.endEditing(true)
            guard let type = cellType.pickType else { return }
            self?.showPickerView(type)
        }
    }
    
    override func setupNavBar() {
        super.setupNavBar()
        
        navigationController?.navBarStyle(.clear)
    }
    
    private func transitionAnimation(_ subtype: CATransitionSubtype) {
        let transition = CATransition()
        transition.type = .push
        transition.subtype = subtype
        transition.duration = 0.25
        containerView.exchangeSubview(at: 1, withSubviewAt: 0)
        containerView.layer.add(transition, forKey: "pushAnimation")
    }
    
    // MARK: - getter
    
    private lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(dismissVc))
        view.addGestureRecognizer(tap)
        
        return view
    }()
    
    private lazy var containerView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = 16
        view.layer.masksToBounds = true
        return view
    }()

    private lazy var drugsListView = UINib.loadViewFrom(SEPrescriptionPickDrugsListView.self)
    
    private lazy var drugsDetailView = UINib.loadViewFrom(SEPrescriptionPickDrugsDetailView.self)

}

extension SEPrescriptionPickDrugsViewController {
    @objc private func dismissVc() {
        dismiss(animated: true, completion: nil)
    }
    
    private func showPickerView(_ type: CommonPickerViewType) {
        let picker = UINib.loadViewFrom(CommonPickerView.self)
        picker.frame = CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: UIScreen.screenHeight)
        picker.show(type: type)
        picker.selectedClosure = { model in
            switch type {
            case .drugFrequency:
                self.viewModel.drugModel.drugFrequency = model.value
            case .drugUsing:
                self.viewModel.drugModel.drugUsing = model.value
            case .drugPosition:
                self.viewModel.drugModel.drugPosition = model.value
            default:
                break
            }
            self.drugsDetailView.viewModel = self.viewModel
        }
    }
}
